package org.dding.core;

import static org.dding.core.management.ObjectProperties.property;

import org.dding.core.management.MBeanFactory;
import org.dding.core.management.ObjectProperties;
import org.dding.core.manager.IModuleManager;
import org.dding.core.manager.IServiceManager;
import org.dding.core.mx.MSCMBeanServer;
import org.dding.core.mx.MSCMBeanServerFactory;
import org.dding.msc.service.management.ServiceContainer;
import org.dding.msc.service.management.ServiceContainerMBean;

public class PlatformMBeanServer {

	public static void initMBean(Object object) {
		MSCMBeanServer mBeanServer = MSCMBeanServerFactory.createMBeanServer();
		Object mbeanImpl = MBeanFactory.createModuleManager(object);

		mBeanServer.registerMBean(MSCMBeanServerFactory.DOMAIN,
				ObjectProperties.properties(
						ObjectProperties.property("type", "module"),
						ObjectProperties.property("name", "container")),
				mbeanImpl);

		if (!(object instanceof IModuleManager))
			return;
		IModuleManager manager = (IModuleManager) object;
		for (IServiceManager serviceManager : manager.getServiceManagers()) {
			ServiceContainerMBean containerMXBean = new ServiceContainer(
					serviceManager);
			mBeanServer.registerMBean(MSCMBeanServerFactory.DOMAIN,
					ObjectProperties.properties(property("type", "msc"),
							property("name", serviceManager.getName())),
					containerMXBean);
		}

	}
}
